package com.cloud.item.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * @author lscl
 * @version 1.0
 * @intro:
 */
@RestController
@RequestMapping("/item")
public class ItemController {

    @Autowired
    private RestTemplate restTemplate;

    /**
     * 测试zuul传递请求头和Cookie
     *
     * @param request
     * @return
     */
    @PostMapping("/testHeader")
    public Map testHeader(HttpServletRequest request) {

        return new HashMap() {{
            put("flag_a", request.getHeader("flag_a"));
            put("flag_b", request.getHeader("flag_b"));
            put("cookie", Arrays.asList(request.getCookies() == null ? "null" : request.getCookies()));
        }};
    }

    @GetMapping("/findOrderById/{orderId}")
    public Map findOrderById(@PathVariable Integer orderId) throws InterruptedException {
        int i = 1 / orderId;
        System.out.println("item..." + System.currentTimeMillis());

        Map resultMap = restTemplate.getForObject("http://order-service/order/" + orderId, Map.class);

        return resultMap;
    }

    @GetMapping("/findOrderAll") public Map findOrderAll() {
        // 模拟线程阻塞
        try {
            Thread.sleep(4000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        Map resultMap = restTemplate.getForObject("http://order-service/order/", Map.class);

        return resultMap;
    }

}

